package net.cardgame.orcalecard;

import java.util.ArrayList;

import jp.jma.oraclecard.R;
import net.cardgame.oraclecard.common.SelectedHistoryListener;
import net.cardgame.orcalecard.bean.HistoryBean;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("ResourceAsColor")
public class HistoryAdapter extends ArrayAdapter<HistoryBean> implements
		OnTouchListener, OnClickListener {
	ArrayList<HistoryBean> listHistory = new ArrayList<HistoryBean>();
	Activity context;
	int x_old = 0, y_old = 0;
	int selectedId = 0;
	boolean ontouch = false;
	SelectedHistoryListener listener;
	boolean clear = false;

	public HistoryAdapter(Activity context, ArrayList<HistoryBean> objects) {
		super(context, R.layout.item_listview_history, objects);
		// TODO Auto-generated constructor stub
		this.listHistory = objects;
		this.context = context;
		this.listener = (SelectedHistoryListener) context;
	}

	static class ViewHolder {
		TextView txtDateTime;
		ImageView imgNumberOfCard;
		TextView txtDeckTitle;
		TextView txtQuestion;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.item_listview_history, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.txtDateTime = (TextView) rowView
					.findViewById(R.id.txtDatetime_history);
			viewHolder.txtDeckTitle = (TextView) rowView
					.findViewById(R.id.txtdecktitle_history);
			viewHolder.txtQuestion = (TextView) rowView
					.findViewById(R.id.txtQuestion_history);
			viewHolder.imgNumberOfCard = (ImageView) rowView
					.findViewById(R.id.img_numberofcard_history);
			rowView.setTag(viewHolder);
		}
		ViewHolder holder = (ViewHolder) rowView.getTag();
		HistoryBean history = listHistory.get(position);
		holder.txtDateTime.setText(history.strDateTime);
		holder.txtDeckTitle.setText(history.deckTitle);
		holder.txtQuestion.setText(history.question);
		rowView.setId(listHistory.get(position).id);
		int size = history.cardBeans.size();
		switch (size) {
		case 1:
			holder.imgNumberOfCard.setImageResource(R.drawable.result_draw1);
			break;
		case 3:
			holder.imgNumberOfCard.setImageResource(R.drawable.result_draw3);
			break;
		case 4:
			holder.imgNumberOfCard.setImageResource(R.drawable.result_draw4);
			break;
		default:
			break;
		}
		rowView.findViewById(R.id.btnDelete_history).setVisibility(
				View.INVISIBLE);
		rowView.setOnTouchListener(this);
		return rowView;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int id = v.getId();
		int x = (int) event.getRawX();
		int y = (int) event.getRawY();
		if (id != selectedId && selectedId != 0) {
			// clear all row focus
			this.notifyDataSetChanged();
			selectedId = 0;
		}
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			x_old = x;
			y_old = y;
			v.setSelected(true);
			break;
		case MotionEvent.ACTION_MOVE:
			if (x - x_old > 10) {
				v.setSelected(true);
				selectedId = v.getId();
				ImageView btnDelete = (ImageView) v
						.findViewById(R.id.btnDelete_history);
				btnDelete.setVisibility(View.VISIBLE);
				ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 1.0f,
						1.0f, Animation.RELATIVE_TO_SELF, 1.0f,
						Animation.RELATIVE_TO_SELF, 0.5f);
				anim.setDuration(500);
				btnDelete.startAnimation(anim);
				btnDelete.setOnClickListener(this);
				return false;
			}
			break;
		case MotionEvent.ACTION_UP:
			if (selectedId == 0)
				v.setSelected(false);
			if (Math.abs(x - x_old) < 3 && Math.abs(y - y_old) < 3) {
				listener.onSelectedItemListener(v.getId());
				v.setSelected(false);
			}
			break;
		case MotionEvent.ACTION_CANCEL:
			if (selectedId == 0)
				v.setSelected(false);
			break;
		default:
			break;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		for (HistoryBean history : listHistory) {
			if (history.id == selectedId) {
				selectedId = 0;
				listHistory.remove(history);
				this.notifyDataSetChanged();
				listener.onDeleteHistory(history);
				return;
			}
		}
	}
}
